#include <elasys_server.h>
#include <aura.h>

#ifndef __CMALLOC_H
#define __CMALLOC_H

#ifndef STDMETHODCALLTYPE
#define STDMETHODCALLTYPE __stdcall
#endif

typedef Int64 TIME;

class CMalloc
{
public:
    CMalloc(){}
    virtual ~CMalloc(){}
public:
    virtual PVoid STDMETHODCALLTYPE Alloc(
            /* [in] */ size_t cb);

    virtual PVoid STDMETHODCALLTYPE Realloc(
            /* [in] */ PVoid pv,
            /* [in] */ size_t cb);

    virtual void STDMETHODCALLTYPE Free(
            /* [in] */ PVoid pv);

    size_t STDMETHODCALLTYPE GetSize(
            /* [in] */ PVoid pv);
};

#endif // __CMALLOC_H


CMalloc* g_pMalloc = NULL;

EXTERN_C void* gAlloc(int size)
{
    return g_pMalloc->Alloc(size);
}

EXTERN_C void* gRealloc(void *pMem, int size)
{
    return g_pMalloc->Realloc(pMem, size);
}

EXTERN_C void gFree(void *pMem)
{
    g_pMalloc->Free(pMem);
}

EXTERN_C TIME _GetTickCount()
{
    return (TIME)0;
}

PVoid CMalloc::Alloc(size_t Size)
{
    return Aura_malloc(Size);
}

PVoid CMalloc::Realloc(PVoid pMem, size_t NewSize)
{
    return Aura_realloc(pMem, NewSize);
}

void CMalloc::Free(PVoid pMem)
{
    Aura_free(pMem);
}

size_t CMalloc::GetSize(PVoid pMem)
{
    return 0;
}


EXTERN_C void StackTrack(int index)
{
    return ;
}

EXTERN_C void HeapMemoryStatus()
{
    return ;
}

EXTERN_C void CheckHeapMemory(TIME TFlagTime)
{
    return ;
}

EXTERN_C int GetMemState(char *pBuffer)
{
    return 0;
}

EXTERN_C int GetHeapState()
{
    return 0;
}

EXTERN_C Boolean HeapSpySwitch(int iState)
{
    return FALSE;
}

EXTERN_C Boolean __stdcall _DllMainCRTStartup(
    PVoid hDllHandle,
    UInt32 dwReason,
    PVoid preserved)
{
    if(DLL_PROCESS_ATTACH == dwReason){
        Aura_printf("Enter mem manager \n");
        g_pMalloc = new CMalloc();
        if (NULL == g_pMalloc) {
            return FALSE;
        }
    } else {
        Aura_printf("Leave mem manager !! \n");
        //asm("int $3");
    }

    return TRUE;
}

EXTERN_C CMalloc *GetGlobalMallocPointer()
{
    return g_pMalloc;
}

